﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class UserDetails : System.Web.UI.Page
    {
        private Applicant _currentApplicant;

        protected void Page_Load(object sender, EventArgs e)
        {
            _currentApplicant = Session["CurrentUser"] as Applicant;

            if (!IsPostBack)
            {
                FillTextBoxes();
            }
        }

        private void FillTextBoxes()
        {
            var user = ApplicantDB.GetApplicantOnId(_currentApplicant.ApplicantId);

            TextBoxFirstName.Text = user.FirstName;
            TextBoxLastName.Text = user.LastName;
            TextBoxEmail.Text = user.Email;
            TextBoxAdress.Text = user.Address;
            TextBoxCity.Text = user.City;
            TextBoxPhone.Text = user.Phone;
            TextBoxZip.Text = user.Zip;
        }

        protected void ButtonSaveChanges_Click(object sender, EventArgs e)
        {
            var user = ApplicantDB.GetAllpApplicantOnIdInclPassword(_currentApplicant.ApplicantId);

            var updatedApplicant = new Applicant();

            if (user != null)
            {
                updatedApplicant = user;

                updatedApplicant.FirstName = TextBoxFirstName.Text;
                updatedApplicant.LastName = TextBoxLastName.Text;
                updatedApplicant.Address = TextBoxAdress.Text;
                updatedApplicant.City = TextBoxCity.Text;
                updatedApplicant.Email = TextBoxEmail.Text;
                updatedApplicant.Phone = TextBoxPhone.Text;
                updatedApplicant.Zip = TextBoxZip.Text;

                ApplicantDB.UpdateApplicant(updatedApplicant);
            }
        }
    }
}